package com.qsbg.controller;


import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qsbg.entity.Activity;
import com.qsbg.entity.vo.*;
import com.qsbg.mapper.ActivityMapper;
import com.qsbg.service.ActivityService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Resource
    private ActivityService activityService;

    /**
     * 增加活动
     * @param activity
     * @return 统一返回类型
     */
    @PostMapping
    private ResultVo addActivity(@RequestBody Activity activity){
        try{
            activity.setCreateTime(new Date());
            activity.setActivityId("ACT"+IdUtil.simpleUUID());
            activity.setEnabled(true);
            boolean save = activityService.save(activity);

            return ResultVo.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.fail(e.toString());
        }
    }

    /**
     * 根据id获取活动信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    private ResultVo getActivity(@PathVariable Integer id){
        try {
            Activity activity = activityService.getById(id);
            return ResultVo.ok(activity);
        }catch (Exception e){
            e.printStackTrace();
            return  ResultVo.fail(e.toString());
        }
    }


    /**
     * 删除活动
     * @param id
     * @return 统一返回类型
     */
    @DeleteMapping("/{id}")
    private ResultVo deleteActivity(@PathVariable Integer id){
        try {
            activityService.removeById(id);
            return ResultVo.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.fail(e.toString());
        }
    }

    /**
     * 修改活动
     * @param activity
     * @return
     */
    @PutMapping
    private ResultVo updateActivity(@RequestBody Activity activity){
        try{
            activity.setUpdateTime(new Date());
            activityService.updateById(activity);
            return ResultVo.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.fail(e.toString());
        }
    }

    /**
     * 分页查询
     * @param activityPageVo
     * @return
     */
    @PostMapping("/getlist")
    public ResultVo findAll(@RequestBody ActivityPageVo activityPageVo) {
        final IPage<Activity> page1 = new Page<>(activityPageVo.getPageNum(), activityPageVo.getPageSize());
        final QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        wrapper.like("activity_name", activityPageVo.getActivityName());
        wrapper.like("operator", activityPageVo.getOperator());
        if (activityPageVo.getStartDate() != null && activityPageVo.getStopDate() != null) {
            wrapper.between("start_time", activityPageVo.getStartDate(), activityPageVo.getStopDate());
        }
        IPage<Activity> activityIPage = activityService.page(page1,wrapper);
        return ResultVo.ok(activityIPage);
    }

    /**
     * 更改enable值
     * @param id
     * @param enabled
     * @return
     */
    @PutMapping("/{id}/{enabled}")
    public ResultVo updateEnable(@PathVariable Integer id,@PathVariable boolean enabled){
        Activity activity = activityService.getById(id);
        activity.setEnabled(enabled);
        if(activityService.updateById(activity)){
            return ResultVo.ok();
        }else {
            return ResultVo.fail("启用状态修改失败!");
        }
    }

}
